package com.gears_software.sxdc.dccx.data.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.model.CPOrderModel;
import com.gears_software.sxdc.dccx.ui.activity.ImmediatelyReserveActivity_;
import com.gears_software.sxdc.dccx.ui.activity.MainActivity;
import com.gears_software.sxdc.dccx.ui.view.PassengerTravelItemView;
import com.gears_software.sxdc.dccx.ui.view.PassengerTravelItemView_;
import com.gears_software.sxdc.dccx.ui.view.XingCheListItemView;
import com.gears_software.sxdc.dccx.ui.view.XingCheListItemView_;
import com.github.mzule.activityrouter.annotation.Router;
import com.github.mzule.activityrouter.router.Routers;
import com.makeramen.roundedimageview.RoundedImageView;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import org.androidannotations.annotations.ViewById;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * Created by chezhenzhen on 2017/6/27.
 * 乘客行程adapter
 */

public class XingChengListAdapter extends RecyclerView.Adapter<XingChengListAdapter.ViewHolder> {
    Context context;
    List<CPOrderModel> list;
    CPOrderModel model;
    int index;

    public XingChengListAdapter(Context context, List<CPOrderModel> list) {
        super();
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_view_passenger_travel, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.txt_tel.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_triptime_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_jiantou_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_yuding_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_xianlu_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_man_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        model = list.get(position);
        if (model.getAvatar() != null && !model.getAvatar().equals("")) {
            holder.img_avatar.setImageURI(Uri.parse(model.getAvatar() + RequestUrl.avatar));
        }
        holder.txt_vehicleno.setText(model.getVehicleno());
        holder.txt_car.setText(model.getCarmodel());
        holder.txt_vehiclecolor.setText(model.getVehiclecolor());
        holder.txt_username.setText(model.getDrivername());
        final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        final String departTime = sf.format(model.getDeparttime());
        holder.txt_date.setText(departTime);
        holder.txt_start.setText(model.getStartpoint());
        holder.txt_end.setText(model.getEndpoint());
        holder.txt_set.setText("剩余 " + model.getPassengernum() + " 座");
        holder.txt_money.setText(String.valueOf(model.getPrice()));
        holder.txt_fenshu.setText("信任分" + model.getLevel());
        if (model.getPassengernum() == 0) {
            holder.ly_yuding.setVisibility(View.GONE);
            holder.txt_man_icon.setVisibility(View.VISIBLE);
        } else {
            holder.ly_yuding.setVisibility(View.VISIBLE);
            holder.txt_man_icon.setVisibility(View.GONE);

        }

        holder.txt_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index = position;
                new MaterialDialog.Builder(context)
                        .title("提示")
                        .titleGravity(GravityEnum.CENTER)
                        .titleColorRes(R.color.colorFontBigGray)
                        .content("您确定要拨打" + " " + list.get(index).getDriverphone())
                        .contentColorRes(R.color.colorFontBigGray)
                        .positiveText("确定")
                        .positiveColor(ContextCompat.getColor(context, R.color.colorFontBlue))
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                AndPermission.with((MainActivity) context)
                                        .requestCode(100)
                                        .permission(Manifest.permission.CALL_PHONE)
                                        .callback(listener)
                                        .rationale(new RationaleListener() {
                                            @Override
                                            public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                                                // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                                                AndPermission.rationaleDialog(context, rationale).
                                                        show();
                                            }
                                        })
                                        .start();
                            }
                        })
                        .negativeText("取消")
                        .negativeColor(ContextCompat.getColor(context, R.color.colorFontGray))
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                        })
                        .buttonsGravity(GravityEnum.CENTER)
                        .show();

            }
        });
        holder.ly_yuding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CPOrderModel orderModel = list.get(position);
                Routers.open(context, "gears_dccx://yuding?orderid=" + orderModel.getId() + "&price=" + orderModel.getPrice() + "&departtime=" + sf.format(orderModel.getDeparttime()) + "&seat=" + orderModel.getPassengernum());
            }
        });
        holder.ly_xianlu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(context)
                        .title("途径路线")
                        .titleGravity(GravityEnum.CENTER)
                        .titleColorRes(R.color.colorFontBigGray)
                        .content(list.get(position).getRoute())
                        .contentColorRes(R.color.colorFontGray)
                        .positiveText("确定")
                        .positiveColor(ContextCompat.getColor(context, R.color.colorFontBlue))
                        .buttonsGravity(GravityEnum.CENTER)
                        .show();
            }
        });
    }

    private PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantedPermissions) {
            // 权限申请成功回调。
            if (requestCode == 100) {
                //打电话
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + list.get(index).getDriverphone()));
                context.startActivity(intent);
            }
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            // 权限申请失败回调。
            if (requestCode == 100) {
                TastyToast.makeText(context, "获取电话权限失败", TastyToast.LENGTH_SHORT, TastyToast.ERROR);

            }
        }
    };


    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txt_tel;
        public TextView txt_triptime_icon;
        public TextView txt_jiantou_icon;
        public TextView txt_yuding_icon;
        public TextView txt_xianlu_icon;
        public SimpleDraweeView img_avatar;
        public TextView txt_vehicleno;
        public TextView txt_car;
        public TextView txt_vehiclecolor;
        public TextView txt_username;
        public TextView txt_date;
        public TextView txt_start;
        public TextView txt_end;
        public TextView txt_set;
        public TextView txt_money;
        public TextView txt_fenshu;
        public LinearLayout ly_yuding;
        public LinearLayout ly_xianlu;
        public TextView txt_man_icon;

        public ViewHolder(View view) {
            super(view);
            txt_tel = (TextView) view.findViewById(R.id.txt_tel);
            txt_triptime_icon = (TextView) view.findViewById(R.id.txt_triptime_icon);
            txt_jiantou_icon = (TextView) view.findViewById(R.id.txt_jiantou_icon);
            txt_yuding_icon = (TextView) view.findViewById(R.id.txt_yuding_icon);
            txt_xianlu_icon = (TextView) view.findViewById(R.id.txt_xianlu_icon);
            img_avatar = (SimpleDraweeView) view.findViewById(R.id.img_avatar);
            txt_vehicleno = (TextView) view.findViewById(R.id.txt_vehicleno);
            txt_car = (TextView) view.findViewById(R.id.txt_car);
            txt_vehiclecolor = (TextView) view.findViewById(R.id.txt_vehiclecolor);
            txt_username = (TextView) view.findViewById(R.id.txt_username);
            txt_date = (TextView) view.findViewById(R.id.txt_date);
            txt_start = (TextView) view.findViewById(R.id.txt_start);
            txt_end = (TextView) view.findViewById(R.id.txt_end);
            txt_set = (TextView) view.findViewById(R.id.txt_set);
            txt_money = (TextView) view.findViewById(R.id.txt_money);
            txt_fenshu = (TextView) view.findViewById(R.id.txt_fenshu);
            txt_man_icon = (TextView) view.findViewById(R.id.txt_man_icon);
            ly_yuding = (LinearLayout) view.findViewById(R.id.ly_yuding);
            ly_xianlu = (LinearLayout) view.findViewById(R.id.ly_xianlu);
        }
    }
}
